/**
  ******************************************************************************
  * File Name          : xc.h
  * Description        : 
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

#ifndef __XC_H
#define __XC_H
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "tskcfg.h"
#include "route_xc_interface.h"

#define XC_RXCMDQUE_LENGTH        8
#define XC_RXCMDQUE_ITEMSIZE      sizeof(Xc_ActionCmdType)

#define XC_ACTCMD_RESET           0x10u
#define XC_ACTCMD_ACTION          0x32u
#define XC_ACTCMD_MARKUPLIMIT     0x54u
#define XC_ACTCMD_UP              0x76u
#define XC_ACTCMD_DOWN            0x98u

/* 测试模式  继电器直连电机 */
/* 面板灯,通过继电器控制 */
#define XC_HARDLIGHT(x)         RELAY03(x)
/* 拉杆推出 */
#define XC_TIEROD_PU()          do{RELAY01(0);RELAY02(0);vTaskDelay(1000);RELAY01(1);}while(0)
/* 拉杆拉入 */
#define XC_TIEROD_DOWN()        do{RELAY01(0);RELAY02(0);vTaskDelay(1000);RELAY02(1);}while(0)
/* 拉杆停止 */
#define XC_TIEROD_STOP()        do{RELAY01(0);RELAY02(0); XC_HARDLIGHT(0);LED1(LED_Off);}while(0)

/* 测试模式  继电器直连电机 */
/* 拉杆推出 */
//#define XC_TIEROD_PU()          do{LED1(LED_On);RELAY01(0);RELAY02(0);RELAY03(0);RELAY04(0);vTaskDelay(1000);RELAY01(1);RELAY04(1);}while(0)
///* 拉杆拉入 */
//#define XC_TIEROD_DOWN()        do{LED1(LED_On);RELAY01(0);RELAY02(0);RELAY03(0);RELAY04(0);vTaskDelay(1000);RELAY02(1);RELAY03(1);}while(0)
///* 拉杆停止 */
//#define XC_TIEROD_STOP()        do{RELAY01(0);RELAY02(0);RELAY03(0);RELAY04(0);LED1(LED_Off);}while(0)

#define Xc_KeyReset()           IN07()
#define Xc_KeyMarkUpLimit()     IN08()

#define Xc_SetPosition(x)       do{WRITE_REG(TIM1->SR, ~(TIM_SR_CC3IF));WRITE_REG(TIM1->CCR3, x);SET_BIT(TIM1->DIER, TIM_DIER_CC3IE);}while(0)
#define Xc_GetPosition()        READ_REG(TIM1->CNT)
#define Xc_ResetPosition()      CLEAR_REG(TIM1->CNT)

typedef enum
{
  XC_STORAGE_TYPE_OTTM = 1,
  XC_STORAGE_TYPE_CPOS = 2,
  XC_STORAGE_TYPE_UPLT = 3,
}Xc_StorageTypeType;

typedef struct
{
  uint8_t  ottm;
  uint16_t crc16;
}Xc_StorageOttmType;

typedef struct
{
  uint8_t  cpos;
  uint16_t crc16;
}Xc_StorageCPosType;

typedef struct
{
  uint16_t uplt;
  uint16_t crc16;
}Xc_StorageUpltType;

typedef struct
{
  uint8_t cmd;
  uint8_t dat;
}Xc_ActionCmdType;

extern const Xc_StorageTypeType Xc_SaveOutTime;
extern const Xc_StorageTypeType Xc_SaveCurrentPosition;
extern const Xc_StorageTypeType Xc_SaveUpLimit;
  
extern volatile uint8_t  Xc_OutTime;
extern volatile uint8_t  Xc_CurrentPosition;
extern volatile uint16_t Xc_UpLimit;

extern void Xc_BspInit(void);
extern uint8_t Xc_CheckOutTime(uint8_t ottm);
extern uint8_t Xc_CheckCurrentPosition(uint8_t cpos);
extern uint8_t Xc_CheckUpLimit(uint16_t uplt);

extern uint8_t Xc_CodeToPercent(uint16_t code);
extern uint16_t Xc_PercentToCode(uint8_t per);
extern void Xc_UpdataCurrentPosition(void);

extern const Xc_ActionCmdType Xc_CmdReset;
extern const Xc_ActionCmdType Xc_CmdMarkUpLimit;
extern const Xc_ActionCmdType Xc_CmdUp;
extern const Xc_ActionCmdType Xc_CmdDown;

extern SemaphoreHandle_t Xc_KeyDetect_Shd;
extern QueueHandle_t Xc_RxCmd_Qhd;

#endif /* __XC_H */
